<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>actioneventwrapper.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_events_actioneventwrapper.js.html">actioneventwrapper.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Action event wrapper implementation.
<a name="line17"></a> * @author eae@google.com (Emil A Eklund)
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.events.actionEventWrapper&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.a11y.aria&#39;);
<a name="line23"></a>goog.require(&#39;goog.a11y.aria.Role&#39;);
<a name="line24"></a>goog.require(&#39;goog.events&#39;);
<a name="line25"></a>/** @suppress {extraRequire} */
<a name="line26"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line27"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.EventWrapper&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line30"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Event wrapper for action handling. Fires when an element is activated either
<a name="line36"></a> * by clicking it or by focusing it and pressing Enter.
<a name="line37"></a> *
<a name="line38"></a> * @constructor
<a name="line39"></a> * @implements {goog.events.EventWrapper}
<a name="line40"></a> * @private
<a name="line41"></a> */
<a name="line42"></a>goog.events.ActionEventWrapper_ = function() {
<a name="line43"></a>};
<a name="line44"></a>
<a name="line45"></a>
<a name="line46"></a>/**
<a name="line47"></a> * Singleton instance of ActionEventWrapper_.
<a name="line48"></a> * @type {goog.events.ActionEventWrapper_}
<a name="line49"></a> */
<a name="line50"></a>goog.events.actionEventWrapper = new goog.events.ActionEventWrapper_();
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Event types used by the wrapper.
<a name="line55"></a> *
<a name="line56"></a> * @type {Array.&lt;goog.events.EventType&gt;}
<a name="line57"></a> * @private
<a name="line58"></a> */
<a name="line59"></a>goog.events.ActionEventWrapper_.EVENT_TYPES_ = [
<a name="line60"></a>  goog.events.EventType.CLICK,
<a name="line61"></a>  goog.userAgent.GECKO ?
<a name="line62"></a>      goog.events.EventType.KEYPRESS : goog.events.EventType.KEYDOWN,
<a name="line63"></a>  goog.events.EventType.KEYUP
<a name="line64"></a>];
<a name="line65"></a>
<a name="line66"></a>
<a name="line67"></a>/**
<a name="line68"></a> * Adds an event listener using the wrapper on a DOM Node or an object that has
<a name="line69"></a> * implemented {@link goog.events.EventTarget}. A listener can only be added
<a name="line70"></a> * once to an object.
<a name="line71"></a> *
<a name="line72"></a> * @param {goog.events.ListenableType} target The target to listen to events on.
<a name="line73"></a> * @param {function(?):?|{handleEvent:function(?):?}|null} listener Callback
<a name="line74"></a> *     method, or an object with a handleEvent function.
<a name="line75"></a> * @param {boolean=} opt_capt Whether to fire in capture phase (defaults to
<a name="line76"></a> *     false).
<a name="line77"></a> * @param {Object=} opt_scope Element in whose scope to call the listener.
<a name="line78"></a> * @param {goog.events.EventHandler=} opt_eventHandler Event handler to add
<a name="line79"></a> *     listener to.
<a name="line80"></a> * @override
<a name="line81"></a> */
<a name="line82"></a>goog.events.ActionEventWrapper_.prototype.listen = function(target, listener,
<a name="line83"></a>    opt_capt, opt_scope, opt_eventHandler) {
<a name="line84"></a>  var callback = function(e) {
<a name="line85"></a>    var listenerFn = goog.events.wrapListener(listener);
<a name="line86"></a>    if (e.type == goog.events.EventType.CLICK &amp;&amp; e.isMouseActionButton()) {
<a name="line87"></a>      listenerFn.call(opt_scope, e);
<a name="line88"></a>    } else if ((e.keyCode == goog.events.KeyCodes.ENTER ||
<a name="line89"></a>        e.keyCode == goog.events.KeyCodes.MAC_ENTER) &amp;&amp;
<a name="line90"></a>        e.type != goog.events.EventType.KEYUP) {
<a name="line91"></a>      // convert keydown to keypress for backward compatibility.
<a name="line92"></a>      e.type = goog.events.EventType.KEYPRESS;
<a name="line93"></a>      listenerFn.call(opt_scope, e);
<a name="line94"></a>    } else if (e.keyCode == goog.events.KeyCodes.SPACE &amp;&amp;
<a name="line95"></a>        e.type == goog.events.EventType.KEYUP &amp;&amp;
<a name="line96"></a>        goog.a11y.aria.getRole(/** @type {!Element} */ (e.target)) ==
<a name="line97"></a>            goog.a11y.aria.Role.BUTTON) {
<a name="line98"></a>      listenerFn.call(opt_scope, e);
<a name="line99"></a>      e.preventDefault();
<a name="line100"></a>    }
<a name="line101"></a>  };
<a name="line102"></a>  callback.listener_ = listener;
<a name="line103"></a>  callback.scope_ = opt_scope;
<a name="line104"></a>
<a name="line105"></a>  if (opt_eventHandler) {
<a name="line106"></a>    opt_eventHandler.listen(target,
<a name="line107"></a>        goog.events.ActionEventWrapper_.EVENT_TYPES_,
<a name="line108"></a>        callback, opt_capt);
<a name="line109"></a>  } else {
<a name="line110"></a>    goog.events.listen(target,
<a name="line111"></a>        goog.events.ActionEventWrapper_.EVENT_TYPES_,
<a name="line112"></a>        callback, opt_capt);
<a name="line113"></a>  }
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Removes an event listener added using goog.events.EventWrapper.listen.
<a name="line119"></a> *
<a name="line120"></a> * @param {goog.events.ListenableType} target The node to remove listener from.
<a name="line121"></a> * @param {function(?):?|{handleEvent:function(?):?}|null} listener Callback
<a name="line122"></a> *     method, or an object with a handleEvent function.
<a name="line123"></a> * @param {boolean=} opt_capt Whether to fire in capture phase (defaults to
<a name="line124"></a> *     false).
<a name="line125"></a> * @param {Object=} opt_scope Element in whose scope to call the listener.
<a name="line126"></a> * @param {goog.events.EventHandler=} opt_eventHandler Event handler to remove
<a name="line127"></a> *     listener from.
<a name="line128"></a> * @override
<a name="line129"></a> */
<a name="line130"></a>goog.events.ActionEventWrapper_.prototype.unlisten = function(target, listener,
<a name="line131"></a>    opt_capt, opt_scope, opt_eventHandler) {
<a name="line132"></a>  for (var type, j = 0; type = goog.events.ActionEventWrapper_.EVENT_TYPES_[j];
<a name="line133"></a>      j++) {
<a name="line134"></a>    var listeners = goog.events.getListeners(target, type, !!opt_capt);
<a name="line135"></a>    for (var obj, i = 0; obj = listeners[i]; i++) {
<a name="line136"></a>      if (obj.listener.listener_ == listener &amp;&amp;
<a name="line137"></a>          obj.listener.scope_ == opt_scope) {
<a name="line138"></a>        if (opt_eventHandler) {
<a name="line139"></a>          opt_eventHandler.unlisten(target, type, obj.listener, opt_capt,
<a name="line140"></a>              opt_scope);
<a name="line141"></a>        } else {
<a name="line142"></a>          goog.events.unlisten(target, type, obj.listener, opt_capt, opt_scope);
<a name="line143"></a>        }
<a name="line144"></a>        break;
<a name="line145"></a>      }
<a name="line146"></a>    }
<a name="line147"></a>  }
<a name="line148"></a>};
</pre>


</body>
</html>
